package com.example.jt.jtrefreshandroid;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements JTRefresh.JTRefreshListener {

    private List<String> data;
    private int i = 0;
    private int times=15;
    private ListView lv;
    private ArrayAdapter adapter1;
    private ArrayAdapter adapter2;
    private JTRefresh jtr;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getSupportActionBar().hide();

        data = new ArrayList<String>();

        lv = new ListView(this);
        adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData());
        //adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData());
        lv.setAdapter(adapter1);
        //可以把自己的一个ListView当作第二个参数
        jtr = new JTRefresh(this,lv);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        addContentView(jtr, lp);

        jtr.setJTRefreshListener(this);
    }

    public List<String> getData(){

        for(int i=0;i<times;i++) {
            data.add("测试数据"+i);
        }

        return data;
    }

    @Override
    public void onRefresh() {
        adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData());
        lv.setAdapter(adapter1);
        jtr.finish();
    }

    @Override
    public void onLoadMore() {
        times+=10;
        adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,getData());
        lv.setAdapter(adapter1);
        System.out.println("加载完成，现在开始执行finish方法！！！！");
        jtr.finish();
    }
}
